﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public partial class LoginDAL : BaseDAL<User_List>, IUser_ListDAL
    {
        private DbContext db = DbContextFactory.Create();

        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<object> GetUser(int id)
        {
            //多表连接例子
            return (from ul in db.Set<User_List>()
                    join ud in db.Set<User_Department>()
                    on ul.fk_Department equals ud.pk_Department
                    where ul.pk_User == id
                    select new { ul, ud.DepartmentName }
                    ).ToList();

            #region 单表条件查询，例子
            // return db.Set<User_List>().Where(n => n.pk_User == id);
            #endregion
        }
        /// <summary>
        /// 登录 查询用户表中是否有该用户
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public User_List GetUser(string UserName, string Password)
        {
            return db.Set<User_List>().SingleOrDefault(n => n.UserName == UserName && n.Password == Password);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="u"></param>
        public void UpdateUser(User_List u)
        {
            db.Set<User_List>().AddOrUpdate(u);
        }
        /// <summary>
        /// 修改用户在线状态
        /// </summary>
        /// <param name="UserID"></param>
        public void UpdateUserRemark(int UserID,string Remark)
        {
            User_List Ul = db.Set<User_List>().SingleOrDefault(n => n.pk_User == UserID);
            Ul.Remark = Remark;
        }

        public string GetDepartment(int pk_Department)
        {
            return db.Set<User_Department>().SingleOrDefault(n => n.pk_Department == pk_Department).DepartmentName;
        }

        public string GetPosition(int fk_Position)
        {
            return db.Set<User_Position>().SingleOrDefault(n => n.pk_Position == fk_Position).Title;
        }
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
